<?php

class ManageTicketSourceController extends PsController {

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
						//'roles' => array('Transactions.ViewOwn'),
					)), parent::accessRules());
	}

	public function moduleAccessRules() {
		return array();
	}
	
	public function actionView($id) {
		$this->render('view', array(
			'ticketSource' => $this->loadTicketSource($id),
		));
	}

	public function actionCreate() {
		$ticketSource = new TicketSource;
		//echo Yii::app()->getRequest()->getIsAjaxRequest();exit;
		if (isset($_POST['TicketSource'])) {
			$ticketSource->setAttributes($_POST['TicketSource']);

			if ($ticketSource->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest()) {
					Yii::app()->end();
				}
			} else {	
				$this->redirect(array('view', 'id' => $ticketSource->id));
			}
		}

		$this->render('create', array( 'ticketSource' => $ticketSource));
	}

	public function actionUpdate($id) {
		$ticketSource = $this->loadTicketSource($id);


		if (isset($_POST['TicketSource'])) {
			$ticketSource->setAttributes($_POST['TicketSource']);

			if ($ticketSource->save()) {
				$this->redirect(array('view', 'id' => $ticketSource->id));
			}
		}

		$this->render('update', array(
				'ticketSource' => $ticketSource,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadTicketSource($id)->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$ticketSource = new TicketSource('search');
		$ticketSource->unsetAttributes();

		if (isset($_GET['TicketSource']))
			$ticketSource->setAttributes($_GET['TicketSource']);

		$this->render('index', array(
			'ticketSource' => $ticketSource,
		));
	}
	
	public function actionAjaxupdate()
	{
		$act = $_GET['act'];
		//echo $act;exit;
		if($act=='doSortOrder')
		{
			$sortOrderAll = $_POST['priority'];
			if(count($sortOrderAll)>0)
			{
				foreach($sortOrderAll as $menuId=>$priority)
				{
					$model=$this->loadModel($menuId);
					$model->priority = $priority;
					$model->save();
				}
			}
		}
		else
		{
			$autoIdAll = $_POST['autoId'];
			if(count($autoIdAll)>0)
			{
				foreach($autoIdAll as $autoId)
				{
					$model=$this->loadTicketSource($autoId);
					if($act=='doDelete'){
						
						if ($model->delete()){
							
						}
					}
					/*if($act=='doActive')
						$model->isActive = '1';
					if($act=='doInactive')
						$model->isActive = '0'; */    
					if($model->save())
						echo 'ok';
					else
						throw new PsPageNotFoundException();
	 
				}
			}
		}
	}
	
	/**
	 * Loads the TicketSource AR model
	 * @param integer $id the primary key
	 * @return TicketSource	 */
	public function loadTicketSource($id) {
		//$ticketSource = TicketSource::model()->ofCurrentUser()->findByPk($id);
		$ticketSource = TicketSource::model()->findByPk($id);

		if ($ticketSource == null) {
			throw new PsPageNotFoundException();
		}

		return $ticketSource;
	}
}